﻿#ifndef __AVOID_BW_CONTROL_TEXT_PACK_HEAD_FILE_INCLUDEDD__
#define __AVOID_BW_CONTROL_TEXT_PACK_HEAD_FILE_INCLUDEDD__
#include <string>
#include <Windows.h>
#include "../ctlText.h"

namespace swui
{
	namespace _ctlInterior
	{
		class textpack
		{
		public:
			textpack() {};
			textpack(textpack&& mov)
				: wstr(std::move(mov.wstr)) {}
			textpack(const textpack& cop)
				: wstr(cop.wstr) {}
		public:
			textpack& operator=(const textpack& pack)
			{
				wstr = pack.wstr;
				return *this;
			}
			textpack& operator=(textpack&& pack)
			{
				wstr = std::move(pack.wstr);
				return *this;
			}
		public:
			std::wstring& stdwstr() { return wstr; }
			const std::wstring& stdwstr()const { return wstr; }
			std::wstring::const_pointer wptr() const { return wstr.c_str(); }
			void set(std::wstring::const_pointer text) { wstr = text; }
			void set(const char* text) {
				int len;
				wstr.resize(len = MultiByteToWideChar(CP_ACP, 0, text, -1, 0, 0));
				MultiByteToWideChar(CP_ACP, 0, text, -1, &wstr[0], len);
			}
			void set(const ctlText& str) {
				if(str.isWideChar())
					set(str.getWideString());
				else set(str.getString());
			}
			void clear() { wstr.clear(); }
			unsigned ansiLength() const
			{
				unsigned len = WideCharToMultiByte(CP_OEMCP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
				return len == 0 ? 0 : len - 1;
			}
			unsigned length() const
			{
				return wcslen(wstr.c_str());
			}
			unsigned get(std::wstring::pointer lpszText, unsigned len)
			{
				if(lpszText == 0) return 0;
				unsigned realLen = length() + 1;
				if(len == 0) len = realLen;
				unsigned cpyLen = len < realLen ? len : realLen;
				for(unsigned pos = 0; pos < cpyLen; ++pos)
					lpszText[pos] = wstr[pos];
				return cpyLen;
			}
			unsigned get(char* lpszText, unsigned len)
			{
				if(lpszText == 0) return 0;
				unsigned realLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
				if(len == 0) len = realLen;
				unsigned cpyLen = len < realLen ? len : realLen;
				WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, lpszText, cpyLen, 0, 0);
				return cpyLen;
			}
			std::string getstr()
			{
				unsigned len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
				std::string str(len, 0);
				WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], len, 0, 0);
				return std::move(str);
			}
			std::wstring getwidestr() { return wstr; }
			bool firstzero() { return wstr.empty() || wstr.front() == 0; }
		private:
			std::wstring wstr;
		};
		inline textpack* createCompatibleTextPack(std::wstring* wstr)
		{
			return reinterpret_cast<textpack*>(wstr);
		}
	}
}


#endif